﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Handrise.CDCable.Mobile.Business;
using HandRise.MBHOP.Controls;

namespace Handrise.CDCable.Mobile.UI
{
    public partial class FrmElseWork :FrmBase
    {
        private Plan plan;

        private PatrolDetail patrolDetail;

        private PatrolInfoService patrolInfoService = new PatrolInfoService();

        private int updateState;  //更新状态 0:新增 1:更新

        public FrmElseWork()
        {
            InitializeComponent();
            ttlbrhead.Text = "其他工作";
            ilstbcup.Images.Add(ResourceImage.确定01);
            ilstbcup.Images.Add(ResourceImage.键盘01);
            ilstbcup.Images.Add(ResourceImage.返回01);
            ilstdown.Images.Add(ResourceImage.确定02);
            ilstdown.Images.Add(ResourceImage.键盘02);
            ilstdown.Images.Add(ResourceImage.返回02);

        }

        private void FrmElseWork_Load(object sender, EventArgs e)
        {
            if (IsReturn)
                LoadCacheData();
            else
                LoadData();

            tbTitle.Text = plan.Title;

            //获取该计划的其他工作详情
            patrolDetail = patrolInfoService.GetOtherWorkTypePatrolDetail(plan.PlanID);

            if (patrolDetail != null)
                tbContent.Text = patrolDetail.Content;

            updateState = patrolDetail == null ? 0 : 1;

        }

        /// <summary>
        /// 加载数据
        /// </summary>
        private void LoadData()
        {
            plan = (Plan)Parameter[0];

        }

        /// <summary>
        /// 加载缓存数据
        /// </summary>
        private void LoadCacheData()
        {
            plan = (Plan)Caches[0];
        }

        private void bcxunjian_OnButtonClick(object sender, int buttonIndex)
        {
          
            switch (buttonIndex)
            {
                case 0:  //确认
                    if (SavePatrolDetail())
                        mssdlgexec.ShowDialog(this, "保存完成", DialogType.Confirm, FormManage.form_Deactivate);

                    break;
                case 1:
                    InputMethod.SHFullScreen(this.Handle, 4);
                    InputMethod.SipShowIM(1);
                    tbContent.Focus();
       

                    break;
                case 2:
                    FormManage.GotoNext("FrmPatrolType", null, null, true);

                    break;




            }
        }

        private bool SavePatrolDetail()
        {
            try
            {
                if (updateState == 0)
                {
                    //新增
                    patrolDetail = new PatrolDetail();
                    patrolDetail.PatrolDetailID = Guid.NewGuid().ToString();
                    patrolDetail.PlanID = plan.PlanID;
                    patrolDetail.WorkType = "其他工作";
                    patrolDetail.Content = tbContent.Text;
                    patrolInfoService.AddProtolDetail(patrolDetail, null, null);
                    updateState = 1;
                }
                else
                {
                    //更新
                    patrolDetail.Content = tbContent.Text;
                    patrolDetail.ReportState = "未上报";
                    patrolInfoService.UpdatePatrolDetail(patrolDetail);
                }

                return true;
            }
            catch (Exception e)
            {
                
                return false ;
            }

        }

        private void btnPhoto_Click(object sender, EventArgs e)
        {
            //如果没有保存巡视详情，线保存巡视详情
            SavePatrolDetail();

            if (patrolDetail == null)
                return;

            LinkFileService linkFileService = new LinkFileService();
            LinkFile linkFile = linkFileService.TaskPicture(patrolDetail.PatrolDetailID);
            FormManage.GotoNext("FrmCamera", new object[] { "FrmElseWork", linkFile }, new object[] { plan }, false);

            ////获取改异常关联的图片个数，如果没有，则启动拍照
            //LinkFileService linkFileService = new LinkFileService();
            //List<LinkFile> linkFiles = linkFileService.GetLinkFileByLinkID("'" + patrolDetail.PatrolDetailID + "'");

            //if (linkFiles.Count == 0)
            //{
            //    LinkFile linkFile = linkFileService.TaskPicture(patrolDetail.PatrolDetailID);
            //    FormManage.GotoNext("FrmCamera", new object[] { "FrmElseWork", linkFile }, new object[] { plan }, false);
            //}
            //else
            //{
            //    //直接显示图片列表
            //    FormManage.GotoNext("FrmPictureList", new object[] { "FrmElseWork","'" + patrolDetail.PatrolDetailID + "'" }, new object[] { plan }, false);

            //}
        }

        private void btnRecord_Click(object sender, EventArgs e)
        {
            //如果没有保存巡视详情，线保存巡视详情
            SavePatrolDetail();

            if (patrolDetail == null)
                return;

            FormManage.GotoNext("FrmRecording", new object[] { "FrmElseWork", patrolDetail.PatrolDetailID }, new object[] {plan}, false);

        }


        
    }
}